<?php

namespace App;

use App\Models\Follower;
use App\Models\Group;
use App\Models\Live;
use App\Models\Site;
use App\Models\Traits\Favorite;
use App\Models\Traits\Favour;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Laravel\Sanctum\HasApiTokens;
use Modules\Edu\Entities\Sign;
use Modules\Edu\Entities\Video;
use Spatie\Permission\Traits\HasRoles;


class User extends Authenticatable
{
    use Notifiable, HasApiTokens, HasRoles, Favour;

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'name', 'email', 'password', 'mobile', 'github', 'qq', 'weibo'
    ];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'password', 'remember_token',
    ];


    protected $casts = [
        'email_verified_at' => 'datetime',
    ];

//图标
    public function getIconAttribute()
    {
        return $this->avatar ?: '/images/avatar.jpg';
    }

//昵称
    public function getNicknameAttribute()
    {
        return $this->name ?? '幸福的小海豚';
    }

//设置密码bcrypt加密
    public function setPasswordAttribute($password)
    {
        $this->attributes['password'] = bcrypt($password);
    }

//所在组关联
    public function group()
    {
        return $this->belongsTo(Group::class);
    }

//本地作用域查询，根据@name,在mobile或name字段中查询
    public function scopeSearch($query, $name)
    {
        //scope 类似于访问器 scope
        if (empty($name)) return $query;
        return $query->where('name', 'like', "%{$name}%")->orWhere('email', 'like', "%{$name}%")->orWhere('mobile', 'like', "%{$name}%")->orWhere('id', $name);
    }

//站点关联，一对多
    public function sites()
    {
        return $this->hasMany(Site::class);
    }

//站点管理员多对多关系
    public function adminSites()
    {
        return $this->belongsToMany(Site::class, 'admin_site');
    }

//所有站点
    public function getAllSitesAttribute()
    {
        return $this->sites->merge($this->adminSites);
    }

//超级管理员
    public function getIsSuperAdminAttribute()
    {
        return $this->id === 1;
    }

    public function make(User $user = null)
    {
        $class = 'Modules\\' . module()['name'] . '\Entities\User';
        //这里需要使用当前系统用户模型id来查找子模块的User模型
        //user和sign是一对多的关联表，通过user获取sign时，使用当前登录用户，可能造成当前用户当天没有签过到，致使获取sign信息时是null,这样signList签到列表，或出现错误.
        return $class::find($user['id'] ?? $this['id']);
    }

//粉丝的关联
    public function fans()
    {
        //粉丝

        //"select * from `users` inner join `followers` on `users`.`id` = `followers`.`follower_id` where `followers`.`user_id` = ?"

        return $this->belongsToMany(User::class, 'followers', 'user_id', 'follower_id')->withTimestamps();
    }

//关注的关联
    public function followers()
    {
        //关注了谁
        //
        //"select * from `users` inner join `followers` on `users`.`id` = `followers`.`user_id` where `followers`.`follower_id` = ?"
        return $this->belongsToMany(User::class, 'followers', 'follower_id', 'user_id')->withTimestamps();
    }

    public function isFollower(User $user)
    {
        //当前登录用户的粉丝 是否有 传入的粉丝ID
        //"select * from `users` inner join `followers` on `users`.`id` = `followers`.`follower_id` where `followers`.`user_id` = ? and `follower_id` = ?"
        return $user->fans()->where('follower_id', $this['id'])->exists();
    }

    //收藏的贴子，视频，课程
    public function getFavoriteModel(string $model)
    {
        $class = $this->model($model);
        return $this->morphedByMany($class, 'favorite', 'favorite');
    }

    protected function model()
    {
        //根据模板名称在edu地址中找到对应模块并通过app()实例化
        return $class = 'Modules\Edu\Entities\\' . request()->model;

    }

    public function live()
    {
        return $this->hasOne(Live::class);
    }


}
